package edu.uc.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;


import edu.uc.bean.JvmData;

import edu.uc.dao.JvmDataDao;

@Repository("jvmDataDao")
public class JvmDataDaoImpl extends BaseDaoImpl<JvmData> implements JvmDataDao{

	
	public Long insert(JvmData bean) {
		Long result = 0L;
		try {
			// 保存
			getTmpl().save(bean);
			// 获取主键
			result = bean.getJvmid();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			
		}
		return result;
	}

	@Override
	public Long delete(Long id) {
		Long result = 0L;
		try {
			// 使用会话,获取特定主键的某个类型的对象
			JvmData instance = getTmpl().get(JvmData.class, id);
			// 如果存在,就删除
			if (instance != null) {
				getTmpl().delete(instance);
			}
			// 正常用提交,且数据存在, 则说明删除成功
			if (instance != null) {
				result = 1L;
			}
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	
	public Long update(JvmData bean) {
		Long result = 0L;

		try {
			getTmpl().update(bean);

			// 正常用提交,且数据存在, 则说明删除成功
			result = 1L;
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	
	public JvmData load(Long id) {
		JvmData bean = null;

		try {
			// 使用会话,获取特定类型的主键对象
			bean = getTmpl().get(JvmData.class, id);

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return bean;
	}

	
	public List<JvmData> list() {
		List<JvmData> vList = new ArrayList<JvmData>();
		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from JvmData ");
		sbHql.append(" order by now_data desc ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();
		try {

			vList = (List<JvmData>) getTmpl().find(hql, params);

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}
		return vList;
	
	}

	@Override
	public Long count() {
		Long result = 0L;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from JvmData ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Long countByName(String name) {
		Long result = 0L;

		name = "%" + name + "%";

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from JvmData where jvmid like ? ");

		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public JvmData loadByName(String name) {
		JvmData bean = null;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from JvmData where jvmid=? ");
		sbHql.append(" order by jvmid desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);
		Object[] params = arrParams.toArray();
		try {

			bean = getTmpl().execute(this.getCallbackBean(JvmData.class, hql, params));

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}

		return bean;
	}

	@Override
	public List<JvmData> pagerUsually(Long pageNum, Long pageSize) {
		List<JvmData> vList = new ArrayList<JvmData>();

		Long vStart = 0L;
		// Long vEnd = 0;

		vStart = (pageNum - 1) * pageSize;// 下标从0开始,所以不要+1
		// vEnd = pageNum * pageSize;
		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from JvmData a1 ");
		sbHql.append(" order by a1.now_data desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();

		try {

			vList = getTmpl().execute(this.getCallbackPager(JvmData.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);

		} finally {

		}

		return vList;
	}
	@Override
	public List<JvmData> pager(Long pageNum, Long pageSize) {
		List<JvmData> vList = new ArrayList<JvmData>();

		Long vStart = 0L;
		// Long vEnd = 0;

		vStart = (pageNum - 1) * pageSize;// 下标从0开始,所以不要+1
		// vEnd = pageNum * pageSize;
		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from JvmData a1 ");
		sbHql.append("where ((a1.heapMemoryUsageUsed)/(a1.heapMemoryUsageMax))>0.6");
		sbHql.append(" order by a1.now_data desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();

		try {

			vList = getTmpl().execute(this.getCallbackPager(JvmData.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);

		} finally {

		}

		return vList;
	}

	@Override
	public List<JvmData> pagerByName(String name, Long pageNum, Long pageSize) {
		List<JvmData> vList = new ArrayList<JvmData>();

		Long vStart = 0L;
		// Long vEnd = 0L;

		vStart = (pageNum - 1) * pageSize;// 不要+1
		// vEnd = pageNum * pageSize;

		name = "%" + name + "%";

		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from JvmData ");
		sbHql.append(" where jvmid like ? ");
		sbHql.append(" order by jvmid desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {
			vList = getTmpl().execute(this.getCallbackPager(JvmData.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return vList;
	}

	
}
